local state = {}
state.name = "FIRING DIRECT"
state.id = 5

if(SERVER) then

  state.startTime = nil
  state.duration = 1
  state.beam = nil

  function state.New()
    return table.Copy(state)
  end

  function state:Run(ag3)
    if(self.startTime == nil) then
      self.startTime = CurTime()
    end
    
    if(CurTime() - self.startTime >= self.duration) then
      return ag3:SetNextState(ag3.States.Idle)
    end
    
    if(self.beam == nil) then
      self.beam = self:CreateBeam(ag3)
    end
    
    return self
  end

  function state:CreateBeam(ag3)
    local beam = ents.Create("energy_beam")

    if(ValidEntity(beam) == false) then
      error("Could not create beam.\n")
    end

    local Start = {}
    Start.Entity = ag3
    Start.offset = ag3:GetEmitterPos() - ag3:GetPos()

    local Damage = {}
    Damage.amount = ag3.Damage.current
    Damage.radius = ag3.Damage.radius

    beam:Setup(ag3, 
               Start, 
               Damage, 
               ag3:GetBeamSpeed(),
               ag3:GetBeamColour())
                   
    beam:Spawn()
    beam:Activate()

    beam:EmitSound(ag3:GetFiringSound(), 100, 100)
    
    return beam
  end
  
end

ENT.States.FiringDirect = state
